home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*---------------------------------------------------------------------------
- * xpg3_wchar.c : sample program which shows the use of wide characters
- * for string manipulation which require locating specific
- * characters
- * Note : this is an XPG/3 way of internationalization
- *
- * this example reads a command line string and outputs two interleaved
- * strings (not useful in real life, but shows how to locate and
- * manipulate strings made of wide characters).
- *
- * Author : Yusuf Attarwala
- * Date : May 93
- *
- *---------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdarg.h>
- #include <ctype.h>
- #include <string.h>
- #include <locale.h>
- #include <wctype.h>
- #include <nl_types.h>
-
-
- nl_catd catd;
-
- void
- usage(pname)
- char *pname;
- {
- char *msg = catgets(catd,1,1,"Usage : %1$s string\n");
- printf(msg,pname);
- }
-
- myExit()
- {
- catclose(catd);
- exit();
- }
-
- int
- main(argc, argv)
- register argc;
- char **argv;
- {
- register int c;
- register wchar_t *s,*o,*p;
- wchar_t *first,*second;
- int i,n;
-
- /* i18n support */
- /* establish a locale, cause locale database to be read in */
- /* passing empty string will cause setlocale to look
- for an environment variable LANG */
-
- (void)setlocale(LC_ALL, "");
-
- /* open a catalogue for messages, etc */
- catd = catopen("xpg3_wchar.cat",0);
-
- /* sanity check */
- if (argc < 2) {
- usage(argv[0]);
- myExit();
- }
-
- /* allocate memory for the wide character string */
- n = strlen(argv[1]);
- if( !(s = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
- printf(catgets(catd,1,2,"Could not allocate memory\n"));
- myExit();
- }
-
- /* convert string to wchar_t */
- if(mbstowcs(s, argv[1], n) < 0) {
- printf(catgets(catd,1,3,"Error in creating wide characters\n"));
- myExit();
- }
-
- /* allocate memory for second string */
- if( !(p = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
- printf(catgets(catd,1,2,"Could not allocate memory\n"));
- myExit();
- }
-
- o = s;
-
- first = o;
- second = p;
-
- /* collect odd and even letters in different strings */
- for(i=0; *s; s += 2,i++) {
- if (!*(s-1) && i > 1) break; /* don't skip the null terminator */
- *o++ = *s;
- *p++ = *(s+1);
- }
-
- /* null terminators */
- mbtowc (o,"\0",1);
- mbtowc (p,"\0",1);
-
- /* print out the interleaved strings */
- printf(catgets(catd,1,4,"Here's the output : "));
-
- putws(first);
- putws(second);
-
- /* close the catalog */
- catclose(catd);
- }
-